<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/5/14
 * Time: 13:03
 */
namespace app\admin\controller;

use app\admin\model\Dream as model;
use app\admin\validate\Dream as validate;
use think\App;
use think\facade\Hook;

class Dream extends Base{
    public function __construct(App $app = null){
        parent::__construct($app);
        $this->validate = new validate;
        $this->model = new model;
    }

    /**
     * 获取列表数据
     * @param array $where
     * @param string $sort
     * @param int $limit
     * @return \think\Response
     * @throws \think\exception\DbException
     */
    public function index($where=[],$sort='',$limit=8){
        $where = $this->_where($where);
        $order = $this->_order($sort);
        $list = $this->model->getList($where,$order,$limit);
        return $this->__s('',$list);
    }

    /**
     * @param int $id
     * @return \think\Response
     */
    public function delete($id=0){
        $info = $this->model->find($id);
        if(!$info){
            return $this->__e("{$this->model->getTableComment()}不存在");
        }
        if(!$info->delete()){
            return $this->__e('删除失败');
        }
        $sql = $info->getLastSql();
        $data = [
            'aid'=>self::$uid,
            'action'=>"删除【{$this->model->getTableComment()}】，编号：{$id}",
            'sql'=>$sql
        ];
        Hook::listen('admin_log',$data);
        return $this->__s('删除成功');
    }

    /**
     * @param int $id
     * @return \think\Response
     */
    public function read($id=0){
        $info = $this->model->find($id);
        if(!$info){
            return $this->__e("{$this->model->getTableComment()}不存在");
        }
        return $this->__s('',$info);
    }

    public function edit($id){
        $info = $this->model->find($id);
        if(!$info){
            return $this->__e("{$this->model->getTableComment()}不存在");
        }
        return $this->__s('',$info);
    }

    /**
     * @param int $id
     * @return \think\Response
     */
    public function update($id=0){
        $info = $this->model->find($id);
        if(!$info){
            return $this->__e("{$this->model->getTableComment()}不存在");
        }
        $data = $this->request->param();
        if(!$info->save($data)){
            return $this->__e('操作失败');
        }
        $sql = $info->getLastSql();
        $data = [
            'aid'=>self::$uid,
            'action'=>"更新【{$this->model->getTableComment()}】，编号：{$id}",
            'sql'=>$sql
        ];
        Hook::listen('admin_log',$data);
        return $this->__s('操作成功');
    }

    /**
     * 审核
     * @param int $id
     * @param int $type
     * @param string $info
     * @route('admin/dream/check','post')
     * @return \think\Response
     * @throws \Exception
     */
    public function check($id=0,$type=1,$info=''){
        $result = $this->model->check($id,$type,$info,$out);
        if(!$result){
            return $this->__e($out);
        }
        $_info = $type==1?'通过':'驳回';
        $data = [
            'aid'=>self::$uid,
            'action'=>"审核{$_info}【{$out['title']}】，编号：{$id}",
            'sql'=>$out['sql']
        ];
        $payload = [
            'title'=>'发布审核',
            'content'=>$out['msg'],
            'payload'=>[
                'type'=>2,
                'id'=>$out['id'],
                'cid'=>$out['cid_type']
            ]
        ];
        (new Push())->pushOne($out['mid'],$payload);
        Hook::listen('admin_log',$data);
        return $this->__s('操作成功');
    }

    /**
     * 查询
     * @param string $q
     * @param int $limit
     * @route('admin/dream/query','get')
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function query($q='',$limit=100){
        $list = $this->model
            ->field('id,title,cid_type as cid')
            ->whereLike('title',"%{$q}%")
            ->where('status','eq',2)
            ->limit($limit)
            ->select();
        return $this->__s('',$list);
    }

    /**
     * 置顶
     * @param $id
     * @param int $type
     * @route('admin/dream/is_top','post')
     * @return \think\Response
     */
    public function is_top($id,$type=0){
        $info = $this->model->find($id);
        if(!$info){
            return $this->__e('内容不存在');
        }
        $info->is_top = $type;
        if(!$info->save()){
            return $this->__e('操作失败');
        }
        return $this->__s('操作成功');
    }
}